package com.wf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.io.unit.DataUnit;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wf.context.BaseContext;
import com.wf.domain.ContractInfo;
import com.wf.domain.HousesInfo;
import com.wf.domain.RentalInfo;
import com.wf.dto.ContractDTO;
import com.wf.dto.ContractMap;
import com.wf.exception.ApartmentException;
import com.wf.mapper.ContractInfoMapper;
import com.wf.mapper.HousesInfoMapper;
import com.wf.mapper.RentalInfoMapper;
import com.wf.service.RentalInfoService;
import com.wf.utils.WordUtils;
import com.wf.vo.HouseDetailsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* @author mpk16
* @description 针对表【rental_info(提交记录表 每30天清空)】的数据库操作Service实现
* @createDate 2024-08-03 13:39:49
*/
@Service
public class RentalInfoServiceImpl extends ServiceImpl<RentalInfoMapper, RentalInfo>
    implements RentalInfoService{

    @Autowired
    private RentalInfoMapper rentalInfoMapper;
    @Autowired
    HousesInfoMapper housesInfoMapper;
    @Autowired
    ContractInfoMapper contractInfoMapper;

    /**
     * 获取签约信息
     * @return
     */
    @Override
    public List<HouseDetailsVO> getSingUp() {

        HashMap<String, Object> map = new HashMap<>();
        map.put("user_id", BaseContext.getCurrentId());
        List<ContractInfo> contractInfos = contractInfoMapper.selectByMap(map);
        List<HouseDetailsVO> houseDetailsVOS = new ArrayList<>();
        contractInfos.forEach(
                contractInfo -> {
                    HouseDetailsVO houseDetailsVO = new HouseDetailsVO();
                    HousesInfo housesInfo = housesInfoMapper.selectById( contractInfo.getHouseId());
                    BeanUtils.copyProperties(housesInfo,houseDetailsVO);
                    BeanUtils.copyProperties(contractInfo,houseDetailsVO);
                    houseDetailsVOS.add(houseDetailsVO);
                }
        );
        return houseDetailsVOS;
    }

    @Override
    public boolean processing(RentalInfo rentalInfo) throws ApartmentException {

        if(rentalInfo.getStatus()==0)
        {

            String realName = rentalInfo.getRealName();

            String idCard = rentalInfo.getIdCard();

            String address = rentalInfo.getAddress();

            String tenantPhone = rentalInfo.getTenantPhone();

            ContractDTO dto = new ContractDTO();
            dto.setTenantName(realName);
            dto.setTenantIdCard(idCard);
            dto.setTenantAddress(address);
            dto.setTenantPhone(tenantPhone);
            ContractMap map = new ContractMap(dto);
            ContractInfo contractInfo = contractInfoMapper.selectById(rentalInfo.getContractId());

            String stencil = contractInfo.getContractStencil();

            try {
                WordUtils.compileHttp(stencil, "word/agreement.pdf",map);
            } catch (Exception e) {
                throw new ApartmentException("生成合同失败");
            }


        }



        return false;
    }
}




